<template>
	<div class="address">
		<AddressHead>我的地址</AddressHead>
		<div class="" v-if="!list.length" style="text-align: center;">
			暂无地址
		</div>
		<van-address-list v-model="chosenAddressId" :list="list" default-tag-text="默认" @add="onAdd" @edit="onEdit" @select="onSelect" />
	</div>
</template>

<script>
	import AddressHead from '../../../components/Head2.vue'
	import Vue from 'vue';
	import {
		AddressList
	} from 'vant';

	Vue.use(AddressList);
	import {
		Toast
	} from 'vant';

	export default {
		data() {
			return {
				chosenAddressId: 1,
				list: [],
			};
		},
		components: {
			AddressHead
		},
		methods: {
			onSelect(item,index){
				console.log(index)
			sessionStorage.setItem('addressd',	JSON.stringify(item))
			this.$router.push('/Confirm')
			},
			onAdd() {
				// Toast('新增地址');
				// sessionStorage.setItem('addressd', JSON.stringify({}))
				this.$router.push('/Edit')
			},
			onEdit(item, index) {
				// Toast('编辑地址:' + index);
				sessionStorage.setItem('addressd', JSON.stringify(this.list[index]))
				setTimeout(() => {
					this.$router.push('/ReEdit')
				}, 300)
			},
		},
		mounted() {
			console.log(this.$store.state.u_id)
			this.$http.post('/address/address', {
				u_id: this.$store.state.u_id
			}).then(res => {
				this.list = res.data.data
				let list2 =[]
				for (var i = 0; i < this.list.length; i++) {
					this.list[i].areaCode = JSON.stringify(this.list[i].areaCode)
					if (this.list[i].isDefault == '1') {
						this.chosenAddressId = this.list[i].id
						this.list[i].isDefault = true
						list2.unshift(this.list[i])
					}else{
						this.list[i].isDefault = ''
						list2.push(this.list[i])
					}
				}
				this.list = list2
			})
		}
	};
</script>

<style scoped="scoped">
	.address {
		margin-top: 3.4375rem;
	}

	.van-address-list__bottom {
		padding: 0;
	}
</style>
